package com.sjm.suchbear.minibookmall.action;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.sjm.suchbear.minibookmall.model.OrderModel;

import java.util.ArrayList;
import java.util.List;

import static com.sjm.suchbear.minibookmall.action.CarManage.deleteCarByuser;

/**
 * Created by Suchbear on 2018/5/5.
 */
public class OrderManage {
    /**
     * 新增订单 如果成功则清空购物车
     *
     * @param db
     * @param userID
     * @param orderstate
     * @param orderprice
     * @param ordername
     * @param orderphone
     * @param orderaddress
     * @return 成功返回订单号 失败返回null
     */
    public static String addOrder(SQLiteDatabase db, String userID, int orderstate, String orderprice, String ordername, String orderphone, String orderaddress) {
        ContentValues values = new ContentValues();
        values.put("userID", userID);
        values.put("orderstate", orderstate);
        values.put("orderprice", orderprice);
        values.put("ordername", ordername);
        values.put("orderphone", orderphone);
        values.put("orderaddress", orderaddress);
        db.insert("mall_order", null, values);
        String sql = "select last_insert_rowid() from mall_order";
        Cursor cursor = db.rawQuery(sql, null);
        String a = null;
        if (cursor.moveToFirst()) {
            a = cursor.getString(0);
        }
        if (a != null) {
            deleteCarByuser(db, userID);
        }
        return a;
    }

    //修改订单状态
    public static void changeOrderState(SQLiteDatabase db, String orderID, int orderstate) {
        ContentValues values = new ContentValues();
        values.put("orderstate", orderstate);
        db.update("mall_order", values, "orderID=?", new String[]{orderID});
    }

    //修改订单信息
    public static void changeOrderInfo(SQLiteDatabase db, String orderID, String ordername, String orderphone, String orderaddress) {
        ContentValues values = new ContentValues();
        values.put("ordername", ordername);
        values.put("orderphone", orderphone);
        values.put("orderaddress", orderaddress);
        db.update("mall_order", values, "orderID=?", new String[]{orderID});
    }

    //根据ID删除订单 取消订单
    public static void deleteOrder(SQLiteDatabase db, String orderID) {
        db.delete("mall_order", "orderID=?", new String[]{orderID});
    }

    //获取订单号获取订单详情
    public static OrderModel getOrderListbyorderid(SQLiteDatabase db, String orderID) {
        OrderModel orderModel = new OrderModel();
        Cursor cursor = db.query("mall_order", new String[]{"orderID", "userID", "orderstate", "orderprice", "ordername", "orderphone", "orderaddress"}, "orderID=?"
                , new String[]{orderID}, null, null, null);
        if (cursor.moveToNext()) {
            orderModel.setOrderID(cursor.getString(0));
            orderModel.setUserID(cursor.getString(1));
            orderModel.setOrderstate(cursor.getString(2));
            orderModel.setOrderprice(cursor.getString(3));
            orderModel.setOrdername(cursor.getString(4));
            orderModel.setOrderphone(cursor.getString(5));
            orderModel.setOrderaddress(cursor.getString(6));
            return orderModel;
        }
        cursor.close();
        return null;
    }

    //获取订单列表根据用户
    public static List<OrderModel> getOrderListbyid(SQLiteDatabase db, String userID) {
        List<OrderModel> orderModels = new ArrayList<>();
        Cursor cursor = db.query("mall_order", new String[]{"orderID", "userID", "orderstate", "orderprice", "ordername", "orderphone", "orderaddress"}, "userID=?"
                , new String[]{userID}, null, null, null);
        if (cursor.getCount() > 0) {
            for (int i = 0; i < cursor.getCount(); i++) {
                cursor.moveToNext();
                OrderModel orderModel = new OrderModel();
                orderModel.setOrderID(cursor.getString(0));
                orderModel.setUserID(userID);
                orderModel.setOrderstate(cursor.getString(2));
                orderModel.setOrderprice(cursor.getString(3));
                orderModel.setOrdername(cursor.getString(4));
                orderModel.setOrderphone(cursor.getString(5));
                orderModel.setOrderaddress(cursor.getString(6));
                orderModels.add(orderModel);
            }
            return orderModels;
        }
        cursor.close();
        return null;
    }

    //获取所有订单列表
    public static List<OrderModel> getOrderList(SQLiteDatabase db) {
        List<OrderModel> orderModels = new ArrayList<>();
        Cursor cursor = db.query("mall_order", new String[]{"orderID", "userID", "orderstate", "orderprice", "ordername", "orderphone", "orderaddress"}, null
                , null, null, null, null);
        if (cursor.getCount() > 0) {
            for (int i = 0; i < cursor.getCount(); i++) {
                cursor.moveToNext();
                OrderModel orderModel = new OrderModel();
                orderModel.setOrderID(cursor.getString(0));
                orderModel.setUserID(cursor.getString(1));
                orderModel.setOrderstate(cursor.getString(2));
                orderModel.setOrderprice(cursor.getString(3));
                orderModel.setOrdername(cursor.getString(4));
                orderModel.setOrderphone(cursor.getString(5));
                orderModel.setOrderaddress(cursor.getString(6));
                orderModels.add(orderModel);
            }
            return orderModels;
        }
        cursor.close();
        return null;
    }

    //获取所有订单状态为state的订单列表
    public static List<OrderModel> getOrderListbyid(SQLiteDatabase db, String userID, int state) {
        List<OrderModel> orderModels = new ArrayList<>();
        Cursor cursor = db.query("mall_order", new String[]{"orderID", "userID", "orderstate", "orderprice", "ordername", "orderphone", "orderaddress"}, "userID=? and orderstate=?"
                , new String[]{userID, String.valueOf(state)}, null, null, null);
        if (cursor.getCount() > 0) {
            for (int i = 0; i < cursor.getCount(); i++) {
                cursor.moveToNext();
                OrderModel orderModel = new OrderModel();
                orderModel.setOrderID(cursor.getString(0));
                orderModel.setUserID(userID);
                orderModel.setOrderstate(cursor.getString(2));
                orderModel.setOrderprice(cursor.getString(3));
                orderModel.setOrdername(cursor.getString(4));
                orderModel.setOrderphone(cursor.getString(5));
                orderModel.setOrderaddress(cursor.getString(6));
                orderModels.add(orderModel);
            }
            return orderModels;
        }
        cursor.close();
        return null;
    }
}
